package days38.title3;

import org.junit.Test;

import java.util.HashSet;

public class Solution {
    // @Test
    // public void test(){
    //     String s = "pwwkew";
    //     int i = lengthOfLongestSubstring(s);
    //     System.out.println("i = " + i);
    // }
    public int lengthOfLongestSubstring(String s) {
        if (s.length()<=1)
            return s.length();
        char[] charArray = s.toCharArray();
        int maxCount = 0;
        int initIndex = 0;
        HashSet<Character> characters = new HashSet<>();
        int i = 0;
        for (; i < charArray.length; i++) {
            characters.add(charArray[i]);
            if ((i-initIndex+1)!=characters.size()){
                i = ++initIndex;
                if (characters.size()>maxCount)
                    maxCount = characters.size();
                characters.clear();
                characters.add(charArray[i]);
            }
        }
        if (i-initIndex+1>maxCount)
            maxCount = i-initIndex;
        return maxCount;
    }
}